<template>
  <div class="content">
    <jc-button
      :btn-arr="['reload', bomCost('createStkDismountDetails') && 'newy', 'remove', 'batchFill', 'pass', 'sub', 'option']"
      :disab-arr="['close', 'cancel', 'com1', 'com2', 'using', 'copy', 'freeze']"
      :fbillno="fbillNo"
      url="tStkDismount/queryStkDismountList"
      @refresh="handleGetUntreated"
      @newly="newAssem"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @toExcel="handleExport"
      @del="delectHandle"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQueryUntreated"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQueryUntreated">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQueryUntreated"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        table-selection
        sortable
        :get-summaries="getSummaries"
        show-summary
        @selectionChange="selectionData"
        @clickRow="detailPurchase"
      >
        <template v-slot:billSlot="clo">
          <el-link type="primary" @click="detailPurchase(clo.scope.row)">{{ clo.scope.row.fbillNo }}</el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:orderSlot="col">
          <el-link type="primary" @click="jumpMateriel1(col.scope.row.fatherNumber)">
            {{ col.scope.row.fatherNumber }}
          </el-link>
        </template>
        <!--    物料编码    -->
        <template v-slot:text2Slot="col">
          <el-link type="primary" @click="jumpMateriel1(col.scope.row.childNumber)">
            {{ col.scope.row.childNumber }}
          </el-link>
        </template>
        <template v-slot:btnState="col">
          <span v-if="col.scope.row.fdocumentstatus === ''" />
          <el-tag v-else :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentstatus }}</el-tag>
        </template>
      </jc-table>
      <!--    分页器-->
      <div class="footer">
        <jc-pagination
          v-show="total > 0"
          :total="total"
          :page.sync="currentPage"
          :limit.sync="size"
          @pagination="handleGetUntreated"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryStkDismountList, delDismounts,
  submitDismounts, notsubmitDismounts,
  auditDismounts, unauditDismounts
} from '@/api/repertory/assembly'
import searData from '@/components/Search/mixin'
import search from '@/components/Search'
import { toMxBmina } from '@/components/ToMxamineState'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'

export default {
  name: 'AssemblyList',
  components: {
    search,
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      shopComm: 1,
      ftype: 57,
      fbillNo: 'fbillNo', // 销售订单号
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      // 表格数据
      tableData: [],
      tableHeader: [
        { label: this.$t('market.billNo'), type: 'bill', prop: 'fbillNo', minWidth: '110px' },
        { label: this.$t('market.placedate'), prop: 'fdate', minWidth: '100px' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentstatus', minWidth: '100px' },
        { label: this.$t('market.stocker'), prop: 'fstockerName' },
        { label: this.$t('market.atherNumber'), type: 'order', prop: 'fatherNumber', minWidth: '120px' },
        { label: this.$t('market.atherModel'), prop: 'fatherModel', minWidth: '110px' },
        { label: this.$t('market.atherQty'), prop: 'fatherQty', align: 'right', width: '80px' },
        { label: this.$t('market.childNumber'), type: 'text2', prop: 'childNumber' },
        { label: this.$t('market.childModel'), prop: 'childModel', minWidth: '100px' },
        { label: this.$t('market.childQty'), prop: 'childQty', align: 'right', width: '80px' }
      ],
      tableHeader1: [],
      val: [], // 多选数据
      fbillno: '', // 修改
      listDate: [], // 选择数据
      tableHeight: 'calc(100vh - 230px)'
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
  },
  methods: {
    // 导出表格-补充人工单价列表
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '组装拆卸单列表')
        } else {
          queryStkDismountList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list.map(item => {
                return (toMxBmina(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '组装拆卸单列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取列表数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = { pageNum: this.currentPage, pageSize: this.size, search: this.searCollData.concat(this.details) }
      await queryStkDismountList(DATA).then(RES => {
        this.tableData = RES.data.list.map(item => {
          return (toMxBmina(item))
        })
        this.total = RES.data.total
      })
      this.loading = false
    },
    // 详情
    detailPurchase(ev) {
      const id = ev.fid
      this.$router.push({ path: `/assemblyEdit/${id}` })
    },
    // 搜索
    handleQueryUntreated(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 新增
    newAssem() {
      this.$router.push({ path: '/createAssembly' })
    },
    // 多选时触发的事件
    selectionData(val) {
      this.val = val
      this.listDate = []
      this.fbillno = val[0].fbillNo
      val.forEach(item => {
        item.fbillNo && this.listDate.push(item.fbillNo)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.listDate }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.listDate = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitDismounts : notsubmitDismounts
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? auditDismounts : unauditDismounts
      this.toExamineFun(api)
    },
    // 删除
    delectHandle() {
      this.$confirm('确定要删除改订单么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        const api = delDismounts
        this.toExamineFun(api)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
